import Vue from 'vue'
import VueRouter from 'vue-router'
import Home from './../pages/home.vue'

Vue.use(VueRouter)

  const routes = [
  {
    path: '/',
    name: 'home',
    component: Home,
    redirect:'/index',
    children:[
      {
        path:'/index',
        name:'index',
        component:() => import('../pages/index.vue')
      },
      {
        path:'/category',
        name:'category',
        component:() => import('../pages/category.vue')
      },
      {
        path:'/cart',
        name:'cart',
        component:() => import('../pages/cart.vue')
      },
      {
        path:'/my',
        name:'my',
        component:() => import('../pages/my.vue')
      }
    ]
  },
  {
    path:'/theme/:id',
    name:'theme',
    component:() => import('../pages/theme.vue')
  },
  {
    path:'/login',
    name:'login',
    component:() => import('../pages/login.vue')
  },
  {
    path:'/goods',
    name:'goods',
    component:() => import('../pages/goods.vue')
  }
]


const router = new VueRouter({
  mode: 'history',
  base: process.env.BASE_URL,
  routes
})


router.beforeEach((to, from, next) => {
  if (to.path === '/login') {
    next();
  } else {
    let token = localStorage.getItem('userId');
    if(!token){
      next('/login');
    }
    next();
  }
});

export default router
